import { createJsapiSignatureAPI } from '@/services/common'
import * as ww from '@wecom/jssdk'

export const registerApi = async (jsApiList: string[]) => {
  const res = await createJsapiSignatureAPI(window.location.href)
  await ww.register({
    agentId: res.data.agentId,
    corpId: res.data.corpId, // 必填，当前用户企业所属企业ID
    jsApiList,
    getConfigSignature: () => {
      return res.data.jsapiSignature
    },
    getAgentConfigSignature: () => {
      return res.data.agentJsapiSignature
    },
  })
}

export const getContextApi = async (): Promise<string> => {
  let qwEntry: string = ''
  await ww.getContext({
    success: (res) => {
      qwEntry = res.entry
    },
  })
  return qwEntry
}

export const getCurExternalContactApi = async (): Promise<string> => {
  let qwExternalUserid: string = ''
  await ww.getCurExternalContact({
    success: (res) => {
      qwExternalUserid = res.userId
    },
  })
  return qwExternalUserid
}
